﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Leadtools.Codecs;

using System.Collections;
using MINERP.BLL;
using MINERP.BLL.LeaderOperate;
using MINERP.LeaderOperate.Controller;
namespace MINERP.LeaderOperate
{
    public partial class UTaskList : UserControl
    {
        public UTaskList()
        {
            InitializeComponent();
            this.splitContainer1.BackColor = Color.White;
        }

        public int WorkerID { get; set; }
        public String WorkerName { get; set; }
        public string Group { get; set; }
        public double Task { get; set; }
        public int AllTask { get; set; }
        public int TaskMax { get; set; }
        public string Path { get; set; }
        public string LoginName
        {
            set;
            get;

        }

        private LeaderTaskBLL bll = new LeaderTaskBLL();
        public void SetInfo()
        {

            this.lab_ID.Text = "工号:" + this.WorkerName;
            this.lab_Name.Text = "姓名:" + this.WorkerName;
            lab_Group.Text = "所属组:" + this.Group;
           
            lab_task.Text = "当前面积:" + this.Task.ToString();
            labelAllTask.Text = "照片数:" + this.AllTask.ToString();
            lab_maxTask.Text = "最大任务数:" + this.TaskMax.ToString();
            if (TaskMax <= 0)
            {
                panel_status.Width = 0;
            }
            else
            {
                panel_status.Width = Convert.ToInt32(87 * (Convert.ToDouble(this.AllTask) / Convert.ToDouble(TaskMax)));
            }
            RasterCodecs.Startup();
            RasterCodecs codecs = new RasterCodecs();
            Leadtools.RasterImage rasterimg = null;
            Leadtools.Codecs.CodecsThumbnailOptions options = Leadtools.Codecs.CodecsThumbnailOptions.Default;
            options.LoadStamp = false;
            options.Resample = true;
            options.MaintainAspectRatio = true;
            //Directory directory =
            try
            {

                //if (File.Exists(this.Path))
                //{
                //    rasterimg = codecs.ReadThumbnail(this.Path, options, 1);
                //    riViewer.Dock = DockStyle.Fill;
                //    riViewer.Image = rasterimg;
                //}

                //else
                //{

                //}
                // riViewer.Image.
               // riViewer.AutoScroll = false;
                //riViewer.DragDrop += new DragEventHandler(riViewer_DragDrop);
                //riViewer.DragEnter += new DragEventHandler(riViewer_DragEnter);
                //riViewer.DoubleClick += new EventHandler(riViewer_DoubleClick);
                //riViewer.Click += new EventHandler(riVewer_Click);
                //  riViewer.MouseClick += new MouseEventHandler(UTaskList_MouseDown);

            }
            catch (Exception ex)
            {

            }
            finally
            {
                RasterCodecs.Shutdown();
            }

        }
        public void SetEvent(Control control)
        {
            control.Click += new EventHandler(control_Click);
            control.DoubleClick += new EventHandler(UTaskList_DoubleClick);
            ControlCollection ccl = control.Controls;
            foreach (Control collection in ccl)
            {
                SetEvent(collection);
            }

        }


        void control_Click(object sender, EventArgs e)
        {
            RushSelect();
            //this.BorderStyle = BorderStyle.Fixed3D;
            //this.BackColor = Color.LightPink;
            //splitContainer1.BackColor = Color.LightPink;
            //SendMessage(this.WorkerID);
            SendClickMessage(this.WorkerID);
        }

        private void riViewer_DragDrop(Object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(ArrayList)))
            {
                e.Effect = DragDropEffects.All;
                this.BorderStyle = BorderStyle.Fixed3D;
                int taskCount = 0;
                string strFlowids = "";
                foreach (var flowId in (ArrayList)(e.Data.GetData(typeof(ArrayList))))
                {
                    //int photoNum = bll.GetPhotoNumByFlowId(Convert.ToInt32(flowId));

                    //taskCount += photoNum;
                    strFlowids = flowId.ToString() + ",";

                }
                if (strFlowids.EndsWith(","))
                {
                   strFlowids= strFlowids.TrimEnd(',');
                }
                taskCount += bll.GetPhotoNumByFlowIds(strFlowids);


                if (taskCount + this.Task > this.TaskMax)
                {
                    if (DialogResult.OK ==
                        MessageBox.Show(
                            "您选中有" + taskCount.ToString() + "个任务超出员工\"" + this.WorkerName + "\"的最大任务量" +
                            this.TaskMax + "，是否继续?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
                    {
                        foreach (var flowId in (ArrayList)(e.Data.GetData(typeof(ArrayList))))
                        {
                            //bll.AssignTsakByWorkerAndOrderID(this.WorkerID, Convert.ToInt32(intOr));//分配任

                            bll.AssignTsakByWorkerAndWorFlowID(this.WorkerID, Convert.ToInt32(flowId));
                        }

                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    foreach (var flowId in (ArrayList)(e.Data.GetData(typeof(ArrayList))))
                    {
                        //bll.AssignTsakByWorkerAndOrderID(this.WorkerID, Convert.ToInt32(intOr));//分配任务
                        bll.AssignTsakByWorkerAndWorFlowID(this.WorkerID, Convert.ToInt32(flowId));

                    }

                }
                this.Task += taskCount;
                rushStatus();//刷新员工状态
                SendRush(this.WorkerID);//更新任务列表
                this.BorderStyle = BorderStyle.FixedSingle;

                //this.AllowDrop = false;
                //this.riViewer.AllowDrop = false;  

            }

        }


        private void UTaskList_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(ArrayList)))
            {
                if (((ArrayList)(e.Data.GetData(typeof(ArrayList)))).Count > 0)
                {
                    e.Data.GetData(typeof(ArrayList));
                    e.Effect = DragDropEffects.All;
                    this.BorderStyle = BorderStyle.Fixed3D;
                    this.BackColor = Color.LightPink;
                    splitContainer1.BackColor = Color.LightPink;
                }

            }
        }

        private void UTaskList_DragLeave(object sender, EventArgs e)
        {
            this.BorderStyle = BorderStyle.FixedSingle;
            this.BackColor = Color.White;
            splitContainer1.BackColor = Color.White;
        }



        public delegate void PostDoubleClick(int workerID);
        public event PostDoubleClick SendMessage;

        public delegate void PostClick(int workerID);
        public event PostClick SendClickMessage;

        public delegate void ReshTask();
        public delegate void ReshTask1(int workerid);
        public event ReshTask1 SendRush;
        public event ReshTask RushSelect;
        private void UTaskList_DoubleClick(object sender, EventArgs e)
        {
            // 取得员工信息
            SendMessage(this.WorkerID);
        }


        public void rushStatus()
        {
            lab_task.Text = "目前任务数:" + this.Task.ToString();
            labelAllTask.Text = "任务总数:" + this.AllTask.ToString();
            panel_status.Width = Convert.ToInt32(87 * (Convert.ToDouble(this.Task) / Convert.ToDouble(TaskMax)));
        }




        private void riViewer_DragEnter(Object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(ArrayList)))
            {
                if (((ArrayList)(e.Data.GetData(typeof(ArrayList)))).Count > 0)
                {
                    e.Effect = DragDropEffects.All;
                    //this.BorderStyle = BorderStyle.Fixed3D;
                    //this.BackColor = Color.LightPink;
                    //splitContainer1.BackColor = Color.LightPink;
                }
                //else
                //{
                //    MessageBox.Show("未选中任何任务", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //}

            }

        }




        private void UTaskList_Load(object sender, EventArgs e)
        {
            SetEvent(splitContainer1);
        }



    }
}
